/*
send form data via ajax and return the data to callback function 
*/
function send_form( name , func )
{
	var url = $('#'+name).attr('action');
	
	var params = {};
	$.each( $('#'+name).serializeArray(), function(index,value) 
	{
		params[value.name] = value.value;
	});
	
	
	$.post( url , params , func );	
}

/*
send form data via ajax and show the return content to pop div 
*/

function send_form_pop( name )
{
	set_form_notice( name , "请稍后......" );
	return send_form( name , function( data ){ show_pop_box( data ); } );
}

/*
send form data via ajax and show the return content in front of the form 
*/
function send_form_in( name )
{	
	set_form_notice( name , "请稍后......" );
	return send_form( name , function( data ){ set_form_notice( name , data ) } );
}


function set_form_notice( name , data )
{
	data = '<span class="label label-important">' + data + '</span>';
	
	if( $('#form_'+name+'_notice').length != 0 )
	{
		$('#form_'+name+'_notice').html(data);
	}
	else
	{
		var odiv = $( "<div class='form_notice'></div>" );
		odiv.attr( 'id' , 'form_'+name+'_notice' );
		odiv.html(data);
		$('#'+name).prepend( odiv );
	} 
	
}


function show_pop_box( data , popid )
{
	if( popid == undefined ) popid = 'lp_pop_box'
	//console.log($('#' + popid) );
	if( $('#' + popid).length == 0 )
	{
		var did = $('<div><div id="' + 'lp_pop_container' + '"></div></div>');
		did.attr( 'id' , popid );
		did.css( 'display','none' );
		$('body').prepend(did);
	} 
	
	if( data != '' )
		$('#lp_pop_container').html(data);
	
	var left = ($(window).width() - $('#' + popid ).width())/2;
	
	$('#' + popid ).css('left',left);
	$('#' + popid ).css('display','block');


}

function hide_pop_box( popid )
{
	if( popid == undefined ) popid = 'lp_pop_box'
	$('#' + popid ).css('display','none');
}


function show_float_box(  url , title )
{
	if( title == null ) title = '设置';

	$('#float_box').off('show');
	$('#float_box').on('show', function () 
	{
  		$('#float_box_title').text(title);
  		$('#float_box .modal-body').load(url);
	});
	$('#float_box .modal-body').html('<div class="muted"><center>Loading</center>');
	$('#float_box').modal({ 'show':true ,'backdrop':'static'});

}

function close_float_box()
{
	$('#float_box').modal('hide');
}

function show_float_iframe(  url , title )
{
	if( title == null ) title = '设置';

	$('#float_box').off('show');
	$('#float_box').on('show', function () 
	{
  		$('#float_box_title').text(title);
  		//$('#float_box .modal-body').load(url);
	})
	$('#float_box .modal-body').html('');
	var did = $('<iframe style="height: 200px; width: 100%; border: none;" src="' +url+ '" scrolling="no"></iframe>');
	$('#float_box .modal-body').prepend(did);
	//$('#float_box .modal-body').html('<div class="muted"><center>Loading</center>');
	$('#float_box').modal({ 'show':true ,'backdrop':'static'});

}

function close_float_iframe()
{
	$('#float_box').modal('hide');
}

function update_asset_info(fileName,type,pid)
{
	$("#divStatus").load( '?a=ajax_update_asset&type='+type+'&fileName='+fileName+'&pid='+pid );
}

function update_template_node_active(nodeid,active,tempid)
{
	$("#ajaxdiv").load( '?a=ajax_update_node_active&nodeid='+nodeid+'&active='+active+'&tempid='+tempid );
}

function update_project_status(pid,directorid,status_num,pass)
{
	
	$("#divStatus").load( '?a=ajax_update_project_status&pid='+pid+'&directorid='+directorid+'&status_num='+status_num+'&pass='+pass );
}

function delete_file(qlty_url,pid,asset_uri,secret,asset_id)
{
	$.post( qlty_url+'delete.php'  , { 'pid':pid,'asset_uri':asset_uri,'secret':secret} , function( data )
	{
		var data_obj = jQuery.parseJSON( data );
		console.log( data_obj  );
		
		if( data_obj.err_code == 0  )
		{
			//文件删除成功，需要将数据库中的数据删除
			$("#divStatus").load('?a=delete_asset&pid='+pid+'&asset_uri='+asset_uri+'&secret'+secret+'&asset_id='+asset_id);
		}
		else
		{
			document.write('删除失败：错误代码'+data_obj.err_code+"，‘"+data_obj.msg+"’");
		}	
	} );

}
/* post demo
$.post( 'url&get var'  , { 'post':'value'} , function( data )
{
	var data_obj = jQuery.parseJSON( data );
	console.log( data_obj  );
	
	if( data_obj.err_code == 0  )
	{
					
	}
	else
	{
		
	}	
} );

*/